<?php

namespace Blog\Controller;
use Common\Controller\AddonController;

class IndexController extends AddonController
{
    protected $config = array(
        'login_auth' => false,
        'ctrl_auth' => false
    );

    public function index()
    {
        $this->_web('title', '烬');
        $model = D('Article');
        $map = array_merge(['status' => 1], $this->make_map());
        $list = $model->where($map)->order('post_date desc,id desc')->findPage();
        $even = []; // 偶数列
        $odd = []; // 奇数列
        foreach ($list['data'] as $key => $row) {
            // 计算奇偶列
            if ($key % 2 == 0) $even[] = $row;
            else $odd[] = $row;
        }
        $this->assign('list', $list['data']);
        $this->assign('even', $even);
        $this->assign('odd', $odd);
        $this->assign('page', $list['html']);
        $this->display();
    }

    public function make_map()
    {
        $cid = I('request.category', 0, 'intval');
        $st_date = I('request.st_date');
        $keyword = '%' . str_replace(' ', '%', I('request.keyword')) . '%';
        $map = [];
        if ($cid) $map['category_id'] = $cid;
        if ($st_date) $map['date_format(post_date,"%Y年%m月")'] = $st_date;
        if ($keyword) $map['title|content'] = ['like', $keyword];
        return $map;
    }
}